package controllers;
import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreServiceFactory;

import sample.appsforyourdomain.AppsForYourDomainClient;

@SuppressWarnings("serial")
public class BlobServeServlet extends HttpServlet {
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
		
		BlobInfoFactory blobInfoFactory = new BlobInfoFactory(DatastoreServiceFactory.getDatastoreService());
		BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
		resp.setContentLength(new Long(blobInfo.getSize()).intValue());
		resp.setHeader("content-type", blobInfo.getContentType());
		resp.setHeader("content-disposition", "attachment;filename=" + blobInfo.getFilename());
		blobstoreService.serve(blobKey, resp);
		
	}
}
